﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Reflection;
using System.Windows.Input;

namespace WpfApplication1
{
    class MTree : TreeView
    {
        private static readonly PropertyInfo IsSelectionChangeActiveProperty= typeof(TreeView).GetProperty("IsSelectionChangeActive",BindingFlags.NonPublic | BindingFlags.Instance);
        static MTree()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MTree), new FrameworkPropertyMetadata(typeof(MTree)));
        }

        private List<MTreeViewItem> _mSelectedItems = new List<MTreeViewItem>();
        public MTreeViewItem CurrentMTreeItem { get; set; }
        public List<MTreeViewItem> MSelectedItems
        {
            get { return _mSelectedItems; }
            set { }
        }
        public void AddMSelectedItem(MTreeViewItem item)
        {
            _mSelectedItems.Add(item);
            CurrentMTreeItem = item;
        }
        public void RemovedMSelectedItem(MTreeViewItem item)
        {
            _mSelectedItems.Remove(item);
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new MTreeViewItem();
        }
    }

    class MTreeViewItem : TreeViewItem
    {
        private static readonly PropertyInfo IsSelectionChangeActiveProperty = typeof(TreeView).GetProperty("IsSelectionChangeActive", BindingFlags.NonPublic | BindingFlags.Instance);

        static MTreeViewItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MTreeViewItem), new FrameworkPropertyMetadata(typeof(MTreeViewItem)));
            //this.DefaultStyleKey = typeof(TreeViewItem);

        }
        internal ItemsControl ParentItemsControl
        {
            get
            {
                return ItemsControl.ItemsControlFromItemContainer(this);
            }
        }




        protected MTree ParentTreeView
        {
            get
            {
                for (ItemsControl control = this.ParentItemsControl; control != null; control = ItemsControl.ItemsControlFromItemContainer(control))
                {
                    MTree view = control as MTree;
                    if (view != null)
                    {
                        return view;
                    }
                }
                return null;
            }
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {
                IsSelectionChangeActiveProperty.SetValue(ParentTreeView, true, null);
                this.IsSelected = true;
            }
            else
            {
                IsSelectionChangeActiveProperty.SetValue(ParentTreeView, false, null);
            }
            base.OnGotFocus(e);
        }


        protected override  void OnSelected(RoutedEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {

            }
            else
            {
                ParentTreeView.MSelectedItems.Where(o=>o!=this).ToList().ForEach(o => o.IsSelected = false);
                ParentTreeView.MSelectedItems.Clear();
            }
            base.OnSelected(e);

            ParentTreeView.AddMSelectedItem(this);
        }

 

 

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new MTreeViewItem();
        }
    }
}
